package com.taxicalculator.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
 * 
 * @author Thomas Maco
 *
 */
public class LocationsSQLite extends SQLiteOpenHelper implements
		ILocationSQLite {

	private static final String LOCATION_LONGITUDE = " LONGITUDE";
	private static final String LOCATION_LATITUDE = "LATITUDE";
	private static final String LOCATION_ID = "_ID";
	private static final String LOCATION_TEXT = "Location";
	private static final String LOCATION_TYPE = "TYPE";
	private static final String LOCATIONS_TABLE = "LOCATIONS";
	/**
	 * 
	 * @param ctx
	 * @param name
	 * @param factory
	 * @param version
	 */
	public LocationsSQLite(Context ctx) {
		super(ctx, "Location.db", null, 1);
		
	}
	/**
	 * creates database with my schema
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		// Use integer of type 0 = from and 1 = to
		String schema = "CREATE TABLE " + LOCATIONS_TABLE + " (" + LOCATION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ LOCATION_TYPE + " INTEGER, " + LOCATION_TEXT + " TEXT," + LOCATION_LATITUDE + " REAL, " + LOCATION_LONGITUDE + " REAL );";
		db.execSQL(schema);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	

	}
	/**
	 * fetches location string text from database to displayed by type
	 * @param int type of location to be fetched from database
	 */
	@Override
	public String getLocation(int type) {
		// return location
		String location = null;
		// query to be ran angst database
		String sqlSelect = "SELECT " + LOCATION_TEXT + " FROM "	+ LOCATIONS_TABLE + " WHERE " + LOCATION_TYPE + " = " + type + ";";
		
		// create cursor to read info from database
		Cursor cursor = getReadableDatabase().rawQuery(sqlSelect, null);

			// moves record to first spot
			if (cursor.moveToFirst() == true){
				// checks to make sure there is a location saved
				if (cursor.getCount() == 1) {
					// saves text from database
					location = cursor.getString(cursor.getColumnIndex(LOCATION_TEXT));
					cursor.close();
				}
			}else {
				if(type == 0){
						location = "1600 Pennsylvania Ave NW, Washington DC 20500";
				}else{
					location = "1234 Main City State";
				}
				
			}
		
		return location;
	}

	/**
	 * Saves Location STring to database with a type done as integer 0 = from
	 * location and 1 = to location
	 * 
	 * @param String
	 *            location text being saved
	 * @param int type type of location
	 */
	@Override
	public void saveLocation(String location, int type, double latitude, double longitude) {
		// saves locations to the Database\
		ContentValues cv = new ContentValues();
		cv.put(LOCATION_TYPE, type);
		cv.put(LOCATION_TEXT, location);
		cv.put(LOCATION_LATITUDE, latitude);
		cv.put(LOCATION_LONGITUDE, longitude);
		
		// query to be ran angst database
		String sqlSelect = "SELECT " + LOCATION_TEXT + " FROM "	+ LOCATIONS_TABLE + " WHERE " + LOCATION_TYPE + " = " + type;

		// create cursor to read info from database
		Cursor cursor = getReadableDatabase().rawQuery(sqlSelect, null);

		// checks to make sure there is a location saved if so deletes the old one before the new location is saved
		if (cursor.getCount() >= 1) {
				deleteLoction(type);
				cursor.close();
				
		}
		//this runs no matter what happens	
		getWritableDatabase().insert(LOCATIONS_TABLE, LOCATION_TEXT, cv);
		
	}
	/**
	 * this method will delete the record stored in database 
	 * @param int type to be deleted
	 */
	@Override
	public void deleteLoction(int type) {

		SQLiteDatabase db = getWritableDatabase();
		db.delete(LOCATIONS_TABLE, LOCATION_TYPE + " =?",
				new String[] { String.valueOf(type) });
		db.close();

	}

}
